package com.aaron.kjviewutils.wechatsift;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecycleViewDividerForList;
import com.aaron.kjviewutils.retrofit.RetrofitUtil;
import com.aaron.kjviewutils.wechatsift.service.WeChatService;
import com.github.ybq.android.spinkit.Style;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者：Aaron on 2017/2/20 15:49
 * 邮箱：kaijin.fang@edianniu.com
 */
public class WeChatFragment extends Fragment {

    @Bind(R.id.weCatagoryRecyclerView)
    RecyclerView weCatagoryRecyclerView;

    private List<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo> weChatCatagorys;
    private WeChatSiftActivity rootAty;
    private View rootView;
    private Call<WeChatCatagoryBean> callCatagory;
    private WeChatAdapter adapter;
    private List<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo> datas;
    private String weChatType;
    private WeChatService weChatService;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (WeChatSiftActivity) getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_wechat, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initContent();
    }

    private void initContent()
    {
        datas = new ArrayList<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo>();
        weChatCatagorys = new ArrayList<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo>();
        // 错列网格布局
        weCatagoryRecyclerView.setLayoutManager(new LinearLayoutManager(rootAty));
        adapter = new WeChatAdapter(rootAty, rootAty, datas);
        weCatagoryRecyclerView.setAdapter(adapter);
        weCatagoryRecyclerView.setItemAnimator(new DefaultItemAnimator()); // 默认动画
        weCatagoryRecyclerView.addItemDecoration(new RecycleViewDividerForList(rootAty, LinearLayoutManager.HORIZONTAL));
        Bundle bundle = getArguments();
        if (bundle != null) {
            weChatType = bundle.getString("cid");
            rootAty.mHandler.postDelayed(runnableLoadData, 1000);
        }
    }

    Runnable runnableLoadData = new Runnable() {
        @Override
        public void run() {
            if(!TextUtils.isEmpty(weChatType))
            {
                if(null!=rootAty.progress)
                {
                    rootAty.progress.showProgress(Style.WAVE);
                    rootAty.progress.showAtLocation(rootAty.toolBar, Gravity.CENTER, 0, 0);
                }
                //获取航班信息
                if(null==rootAty.retrofit)
                {
                    rootAty.retrofit = RetrofitUtil.obtainRetrofit();
                }
                weChatService = rootAty.retrofit.create(WeChatService.class);
                callCatagory = weChatService.obtainWeChatByCid(weChatType);
                callCatagory.enqueue(new Callback<WeChatCatagoryBean>() {
                    @Override
                    public void onResponse(Call<WeChatCatagoryBean> call, Response<WeChatCatagoryBean> response) {
                        callCatagory.cancel();
                        if (null != rootAty.progress) {
                            rootAty.progress.dismissView();
                        }
                        WeChatCatagoryBean weChatCatagory = response.body();
                        if ("200".equals(weChatCatagory.retCode)) {
                            //获取信息,绘制界面
                            if(null!=weChatCatagory)
                            {
                                List<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo> weChatCatagorys = weChatCatagory.result.list;
                                if(null!=weChatCatagorys&&!weChatCatagorys.isEmpty())
                                {
                                    datas.clear();
                                    datas.addAll(weChatCatagorys);
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        } else {
                            rootAty.showMsg(weChatCatagory.msg);
                        }
                    }

                    @Override
                    public void onFailure(Call<WeChatCatagoryBean> call, Throwable t) {
                        callCatagory.cancel();
                        if (null != rootAty.progress) {
                            rootAty.progress.dismissView();
                        }
                        rootAty.showMsg("加载数据失败");
                    }
                });
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(null!=rootAty.mHandler)
        {
            rootAty.mHandler.removeCallbacks(runnableLoadData);
        }
        if(null!=rootAty.progress)
        {
            rootAty.progress.dismissView();
        }
        if(null!=callCatagory)
        {
            callCatagory.cancel();
        }
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
